package edu.yuhf.web.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import edu.yuhf.domain.Permission;
import edu.yuhf.service.PermissionServiceImpl;
import edu.yuhf.service.iface.PermissionService;

@WebFilter(dispatcherTypes = {DispatcherType.FORWARD }
					, urlPatterns = { "/views/main.jsp" })
public class PermissionCheckFilter implements Filter {

	public void destroy() {
	}


	public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request =(HttpServletRequest)req;
		HttpSession session=request.getSession();
		if(session.getAttribute("name")!=null) {
			PermissionService permissionService=new PermissionServiceImpl();
			List<Permission> permissions=permissionService.getPermissions((String)session.getAttribute("name"));
			session.setAttribute("permissions", permissions);
		}
		chain.doFilter(req, response); 
	}


	public void init(FilterConfig fConfig) throws ServletException {
	}

}
